package cn.neusoft.edu.his.demo.controller;

import cn.neusoft.edu.his.demo.po.Department;
import cn.neusoft.edu.his.demo.vo.PatientCases;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.neusoft.edu.his.demo.common.Result;
import cn.neusoft.edu.his.demo.mapper.DoctorWorkMapper;
import cn.neusoft.edu.his.demo.mapper.PatientDoctorMapper;
import cn.neusoft.edu.his.demo.po.DoctorWork;
import cn.neusoft.edu.his.demo.po.PatientDoctor;
import cn.neusoft.edu.his.demo.po.Personage;

import javax.xml.crypto.Data;

@CrossOrigin
@Api("PatientDoctorController相关的api")
@RestController
public class PatientDoctorController {
    @Autowired
    PatientDoctorMapper patientDoctorMapper;
    @Autowired
    DoctorWorkMapper doctorWorkMapper;
    @RequestMapping("/addPatientDoctor")
    public int addpatient(PatientDoctor patient){
    	DoctorWork dw=new DoctorWork();
    	dw.setDay(patient.getDay());
    	dw.setDocId(patient.getDocId());   	
    	if(doctorWorkMapper.updateCountLeftById(dw)>0)
    		return patientDoctorMapper.insert(patient);
    	else
    		return -1;//没有剩余的票
    }
    @GetMapping("/getPersonage")
    public Result getpersonage(Personage personage){
        List<Personage> list = patientDoctorMapper.getpersonage(personage);
        return Result.ok(list);
    }

    @GetMapping("/getPatientDoctorByDoctorId")
    public Result getPatientDoctorById(PatientDoctor patientDoctor){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date());
        patientDoctor.setDay(date);
        System.out.println(patientDoctor);
        QueryWrapper<PatientDoctor> wrapper=new QueryWrapper<PatientDoctor>() ;
        wrapper.eq("doc_id",patientDoctor.getDocId());
        List<PatientDoctor> list = patientDoctorMapper.selectList(wrapper);
        return Result.ok(list);
    }
    @GetMapping("/getInfo")
    public Result getInfo(PatientCases patientCases){
        QueryWrapper<PatientDoctor> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id",patientCases.getPatientId())
                .eq("doc_id",patientCases.getDocId());
        PatientDoctor patientDoctor = patientDoctorMapper.selectOne(wrapper);
        return Result.ok(patientDoctor);
    }


    @PostMapping("/setPatientInfo")
    public Result setPatientInfo(@RequestBody PatientDoctor PatientDoctor){
        QueryWrapper<PatientDoctor> Wrapper = new QueryWrapper<>();
        Wrapper.eq("patient_id",PatientDoctor.getPatientId())
                        .eq("doc_id",PatientDoctor.getDocId());
        patientDoctorMapper.update(PatientDoctor,Wrapper);
        System.out.println(PatientDoctor);
        return Result.ok("ok");
    }
    
    @GetMapping("/deletePatientDoctor")
    public Result deletePatientDoctor(PatientDoctor doctor){
    	doctor=patientDoctorMapper.selectById(doctor.getId());
    	DoctorWork dw=new DoctorWork();
    	dw.setDay(doctor.getDay());
    	dw.setDocId(doctor.getDocId());   
    	doctorWorkMapper.updateCountLeftById2(dw);
        int i = patientDoctorMapper.deleteById(doctor.getId());
        if(i>0){
            return Result.ok("删除成功");
        }else
            return Result.fail("删除失败");
    }
}
